package fun.codedesign.yinxue.util;

import java.io.IOException;
import java.io.InputStream;

public class ResourceUtil {

    /**
     * eg：resource目录下的 manager.php， 不加 /
     *
     * @param resourcePath
     * @return
     */
    public static String getResourceAsText(String resourcePath) {
        InputStream is = getResourceAsInputStream(resourcePath);
        try {
            byte[] bytes = new byte[is.available()];
            is.read(bytes);
            String text = new String(bytes, "utf-8");
            LogUtil.info(ResourceUtil.class, "初始资源文件内容:\n" + text);
            return text;
        } catch (IOException e) {
            LogUtil.error(ResourceUtil.class, "加载资源文件失败", e);
            return "";
        }
    }

    public static InputStream getResourceAsInputStream(String resourcePath) {
        return Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath);
    }
}
